//
//  ImgShowViewController.m
//  Project-Movie
//
//  Created by Minr on 14-11-14.
//  Copyright (c) 2014年 Minr. All rights reserved.
//

#import "ImgShowViewController.h"
//#import "MRImgShowView.h"
#import "LXActionSheet.h"

@interface ImgShowViewController ()<UIScrollViewDelegate,LXActionSheetDelegate>
{
    UIScrollView *scrollviewP; //添加photo的scroll
    int currentNumber; //当前的页数
    //LXActionSheet *actionSheetDelete;
}

@end

@implementation ImgShowViewController

- (id)initWithSourceData:(NSMutableArray *)data withIndex:(NSInteger)index{
    
    self = [super init];
    if (self) {
        _data = [NSMutableArray array];
        _data = data;
        _index = index;
        currentNumber = (int)_index;
        //NSLog(@"%@",_data);
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = WQColor(28, 26, 28);
    self.title = @"图片列表";
    
    //设置导航栏为半透明
    self.navigationController.navigationBar.translucent = YES;
    // 隐藏标签栏
    self.tabBarController.tabBar.hidden = YES;

    // 隐藏导航栏
    self.navigationController.navigationBarHidden = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = KDColor;//[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    
    // 添加导航栏退回按钮
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    //添加导航栏删除按钮
    UIBarButtonItem *deleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(selectTheDeletePhoto)];
    
    self.navigationItem.rightBarButtonItem = deleItem;
    self.navigationItem.leftBarButtonItem = backItem;
    //设置导航栏颜色
    self.navigationController.navigationBar.tintColor = KDColor;//[UIColor whiteColor];
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    
    //创建scrollview
    [self creatImgShow];
    
    // 设置数据源
    [self setImgSource:_data];
    
    //滚动到指定的位置去
    [scrollviewP setContentOffset:CGPointMake(self.view.frame.size.width * _index, 0) animated:YES];
}

#pragma mark - 点击删除按钮弹出选择框
-(void)selectTheDeletePhoto
{
    //点击选择是否删除照片 0-删除 1-取消
    LXActionSheet *actionSheetDelete = [[LXActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"删除"]];
    //[actionSheetDelete showInView:self.view];
    [self.navigationController.view addSubview:actionSheetDelete];
}

#pragma mark - LXActionSheetDelegate代理
- (void)didClickOnButtonIndex:(NSInteger *)buttonIndex
{
    if(buttonIndex == 0)
    {
        [self deleteThePhoto];
    }
    else
    {
        return;
    }
}

#pragma mark - 删除当前图片
-(void)deleteThePhoto
{
    //没有照片发送通知
    //取出当前要删除的照片
    //UIImage *imm = nil;
    //imm = _data[currentNumber];
    NSString *curnum = [NSString stringWithFormat:@"%d",currentNumber];
    //NSLog(@"### %@", curnum);
    NSDictionary *userInfo2 = [[NSDictionary alloc] initWithObjectsAndKeys:curnum,@"curz",nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteP123456" object:nil userInfo:userInfo2];
    
    if(_data.count == 1)
    {
        [self backAction];
        return;
    }
    
    [_data removeObjectAtIndex:currentNumber];
    //重新设置数据源
    [self setImgSourceAgain:_data];
}

// 初始化视图
- (void)creatImgShow
{
    scrollviewP = [[UIScrollView alloc] initWithFrame:self.view.frame];
    // 设置代理
    scrollviewP.delegate = self;
    // 不显示滚动条
    scrollviewP.showsHorizontalScrollIndicator = NO;
    scrollviewP.showsVerticalScrollIndicator = NO;
    
    // 设置分页显示
    scrollviewP.pagingEnabled = YES;
    
    //设置背景颜色
    scrollviewP.backgroundColor = [UIColor clearColor];
    scrollviewP.contentSize = CGSizeMake(self.view.frame.size.width * _data.count, self.view.frame.size.height);
    
    //NSLog(@"%@",_data);
    [self.view addSubview:scrollviewP];
}

#pragma mark -setAttribute
// 设置数据源
- (void)setImgSource:(NSMutableArray *)imgSource{
    
    if (_data != imgSource) {
        _data = imgSource;
    }
    scrollviewP.contentSize = CGSizeMake(self.view.frame.size.width * _data.count, self.view.frame.size.height);
    //NSLog(@"%@",_data);
    for(int i = 0; i < _data.count; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        // 设置图片显示样式
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = _data[i];
        [scrollviewP addSubview:imageView];
        //NSLog(@"1");
    }
    //NSLog(@"%lu",(unsigned long)_imgSource.count);
}

#pragma mark - 重新设置数据源
-(void)setImgSourceAgain:(NSMutableArray *)imgSource
{
    scrollviewP.contentSize = CGSizeMake(self.view.frame.size.width * _data.count, self.view.frame.size.height);
    currentNumber = scrollviewP.contentOffset.x / self.view.frame.size.width;
    //将原来的图片删除
    //NSLog(@"%lu",(unsigned long)self.subviews.count);
    for(int i = (int)scrollviewP.subviews.count - 1; i >= 0; i--)
    {
        [scrollviewP.subviews[i] removeFromSuperview];
    }
    
    //重新生成新的照片
    for(int i = 0; i < _data.count; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        // 设置图片显示样式
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = _data[i];
        [scrollviewP addSubview:imageView];
    }
}

#pragma mark -UIGestureReconginzer
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // 隐藏导航栏
    [UIView animateWithDuration:0.3 animations:^{
        self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
    }];
}

#pragma mark -UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //当前的页数
    currentNumber = scrollView.contentOffset.x / self.view.frame.size.width;
    //NSLog(@"^^^ %d",currentNumber);
}

#pragma mark -NavAction
- (void)backAction
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"delete12345" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"deleteP123456" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"noPhoto1234" object:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com 
